const jwt = require('jsonwebtoken');
const config = require('../config');

/**
 * 生成JWT令牌
 * @param {Object} payload - 令牌载荷数据
 * @param {Object} options - 令牌选项
 * @returns {string} JWT令牌
 */
const generateToken = (payload, options = {}) => {
  const defaultOptions = {
    expiresIn: config.jwt.expiresIn || '24h'
  };

  return jwt.sign(
    payload,
    config.jwt.secret,
    { ...defaultOptions, ...options }
  );
};

/**
 * 验证JWT令牌
 * @param {string} token - JWT令牌
 * @returns {Object} 解码后的载荷数据
 */
const verifyToken = (token) => {
  return jwt.verify(token, config.jwt.secret);
};

module.exports = {
  generateToken,
  verifyToken
}; 